#include "DDZGameScence.h"
#include "Game/GameLib.h"
#include "CMD_DDZ.h"
#include "DDZPlayer.h"
#include "GameLib/JniCross/JniFun.h"
#include "DDZSoundFun.h"
#include "Game/Sound/ArmFun.h"
#include "DDZGameLogic.h"
void DDZGameScence::initButton()
{
	WidgetManager::addButtonCB("DDZButton_Ready", this, button_selector(DDZGameScence::DDZButton_Ready));
	WidgetManager::addButtonCB("DDZButton_OnceAgain", this, button_selector(DDZGameScence::DDZButton_Ready));
	WidgetManager::addButtonCB("DDZButton_OutCard", this, button_selector(DDZGameScence::DDZButton_OutCard));
	WidgetManager::addButtonCB("DDZButton_PassCard", this, button_selector(DDZGameScence::DDZButton_PassCard));
	WidgetManager::addButtonCB("DDZButton_TiShi", this, button_selector(DDZGameScence::DDZButton_TiShi));
	WidgetManager::addButtonCB("DDZButton_RefreshHandCard", this, button_selector(DDZGameScence::DDZButton_RefreshHandCard));
	WidgetManager::addButtonCB("Button_TalkDefine", this, button_selector(DDZGameScence::DDZButton_TalkDefine));
	WidgetManager::addButtonCB("Button_Send_TalkStr", this, button_selector(DDZGameScence::DDZButton_Send_TalkStr));

	WidgetManager::addButtonCB("HNMJButton_BiaoQing0", this, button_selector(DDZGameScence::DDZHNMJButton_BiaoQing));
	WidgetManager::addButtonCB("HNMJButton_BiaoQing1", this, button_selector(DDZGameScence::DDZHNMJButton_BiaoQing));
	WidgetManager::addButtonCB("HNMJButton_BiaoQing2", this, button_selector(DDZGameScence::DDZHNMJButton_BiaoQing));
	WidgetManager::addButtonCB("HNMJButton_BiaoQing3", this, button_selector(DDZGameScence::DDZHNMJButton_BiaoQing));
	WidgetManager::addButtonCB("HNMJButton_BiaoQing4", this, button_selector(DDZGameScence::DDZHNMJButton_BiaoQing));
	WidgetManager::addButtonCB("HNMJButton_BiaoQing5", this, button_selector(DDZGameScence::DDZHNMJButton_BiaoQing));
	WidgetManager::addButtonCB("HNMJButton_BiaoQing6", this, button_selector(DDZGameScence::DDZHNMJButton_BiaoQing));
	WidgetManager::addButtonCB("HNMJButton_BiaoQing7", this, button_selector(DDZGameScence::DDZHNMJButton_BiaoQing));

	WidgetManager::addButtonCBBegin("HNMJButton_Talk", this, button_selector(DDZGameScence::DDZHNMJButton_TalkBegin));
	WidgetManager::addButtonCB("HNMJButton_Talk", this, button_selector(DDZGameScence::DDZHNMJButton_TalkEnd));

	WidgetManager::addButtonCB("HNMJButton_Msg", this, button_selector(DDZGameScence::DDZHNMJButton_Msg));

	WidgetManager::addButtonCB("PDKButton_RobDizhu1", this, button_selector(DDZGameScence::DDZButton_RobDizhu));
	WidgetManager::addButtonCB("PDKButton_RobDizhu2", this, button_selector(DDZGameScence::DDZButton_RobDizhu));
	WidgetManager::addButtonCB("PDKButton_RobPass1", this, button_selector(DDZGameScence::DDZButton_RobDizhu));
	WidgetManager::addButtonCB("PDKButton_RobPass2", this, button_selector(DDZGameScence::DDZButton_RobDizhu));
	WidgetManager::addButtonCB("PDKButton_GetDizhu1", this, button_selector(DDZGameScence::DDZButton_GetDizhu));
	WidgetManager::addButtonCB("PDKButton_GetDizhu2", this, button_selector(DDZGameScence::DDZButton_GetDizhu));
	WidgetManager::addButtonCB("PDKButton_GetDizhu3", this, button_selector(DDZGameScence::DDZButton_GetDizhu));
	WidgetManager::addButtonCB("PDKButton_GetPass", this, button_selector(DDZGameScence::DDZButton_GetDizhu));
}

void DDZGameScence::DDZHNMJButton_Msg(cocos2d::Ref*, WidgetUserInfo* pUserInfo)
{
	if (!IsInGame())
	{
		return;
	}
	WidgetFun::setVisible(this, "GameTalkPlane", true);
}

void DDZGameScence::DDZButton_Ready(cocos2d::Ref*, WidgetUserInfo*)
{
	if (!IsInGame())
	{
		return;
	}
	sendReady();
}

void DDZGameScence::DDZButton_OutCard(cocos2d::Ref*, WidgetUserInfo* pUserInfo)
{
	if (!IsInGame())
	{
		return;
	}
	if (m_pLocal->sendOutCard())
	{
		m_pLocal->showBaoDan((m_pLocal->m_nHandCardNum == 1));
		showActionNode(false);
	}
}

void DDZGameScence::DDZButton_PassCard(cocos2d::Ref*, WidgetUserInfo* pUserInfo)
{
	SendSocketData(SUB_C_PASS_CARD, 0, 0);
	m_pLocal->downAllCard();
}

void DDZGameScence::DDZButton_TiShi(cocos2d::Ref*, WidgetUserInfo*)
{
	if (!IsInGame())
	{
		return;
	}
	onOutCardTips();
}

void DDZGameScence::DDZButton_RefreshHandCard(cocos2d::Ref*, WidgetUserInfo*)
{
	if (!IsInGame())
	{
		return;
	}
	m_pLocal->showHandCard();
}


void DDZGameScence::DDZHNMJButton_TalkBegin(cocos2d::Ref*, WidgetUserInfo*)
{
	if (!IsInGame())
	{
		return;
	}
	if (WidgetFun::isWidgetVisble(this, "TalkImagic"))
	{
		JniFun::stopSoundRecord();
		WidgetFun::setVisible(this, "TalkImagic", false);
		return;
	}
	rootNode = this;
	SoundFun::Instance().PaseBackMusic();
	WidgetFun::setVisible(this, "TalkImagic", true);
	JniFun::startSoundRecord();
	int iTimeID = TimeManager::Instance().addCerterTimeNode(TIME_CALLBACK_Node(DDZGameScence::DDZTimeEnd_TalkEnd, this), 5.0f, WidgetFun::getChildWidget(this, "TalkImagic"))->iIdex;
	WidgetFun::setWidgetUserInfo(this, "TalkImagic", "TimeID", utility::toString(iTimeID));
}

void DDZGameScence::DDZTimeEnd_TalkEnd(cocos2d::Node* node)
{
	if (!IsInGame())
	{
		return;
	}
	SoundFun::Instance().OnResumeBackMusic();
	if (!node->isVisible())
	{
		return;
	}
	int iTimeID = utility::parseInt(WidgetFun::getWidgetUserInfo(node, "TimeID"));
	TimeManager::Instance().removeByID(iTimeID);
	node->setVisible(false);
	std::string kFileName = JniFun::stopSoundRecord();
	sendTalkFile(m_pLocal->GetChairID(), kFileName);
}

void DDZGameScence::DDZHNMJButton_TalkEnd(cocos2d::Ref*, WidgetUserInfo*)
{
	if (!IsInGame())
	{
		return;
	}
	DDZButton_TalkEnd(NULL, NULL);
}

void DDZGameScence::DDZButton_TalkEnd(cocos2d::Ref*, WidgetUserInfo*)
{
	if (!IsInGame())
	{
		return;
	}
	SoundFun::Instance().OnResumeBackMusic();
	if (!WidgetFun::isWidgetVisble(rootNode, "TalkImagic"))
	{
		return;
	}
	int iTimeID = utility::parseInt(WidgetFun::getWidgetUserInfo(rootNode, "TalkImagic", "TimeID"));
	TimeManager::Instance().removeByID(iTimeID);
	WidgetFun::setVisible(rootNode, "TalkImagic", false);
	std::string kFileName = JniFun::stopSoundRecord();
	sendTalkFile(m_pLocal->GetChairID(), kFileName);
	ssize_t iSize = 0;
	std::string kDestFile = kFileName;
#if CC_TARGET_PLATFORM != CC_PLATFORM_ANDROID
	kDestFile = cocos2d::CCFileUtils::getInstance()->getWritablePath() + utility::toString(time(NULL), ".arm");
	ArmFun::WavToArm(kFileName.c_str(), kDestFile.c_str());
#endif
	CMD_GR_C_TableTalk kNetInfo;
	kNetInfo.cbChairID = m_pLocal->GetChairID();
	kNetInfo.cbType = CMD_GR_C_TableTalk::TYPE_FILE;
	unsigned char* pData = cocos2d::CCFileUtils::sharedFileUtils()->getFileData(kDestFile, "rb", &iSize);
	if (!pData)
	{
		//NoticeMsg::Instance().ShowTopMsg("Record Error");
		return;
	}
	kNetInfo.cbChairID = m_pLocal->GetChairID();
	memcpy(kNetInfo.strTalkData, pData, iSize);
	free(pData);
	kNetInfo.strTalkSize = iSize;

	DDZGameLogic::Instance().lastTalk->cbChairID = kNetInfo.cbChairID;
	DDZGameLogic::Instance().lastTalk->cbType = CMD_GR_C_TableTalk::TYPE_FILE;
	DDZGameLogic::Instance().lastTalk->strTalkSize = kNetInfo.strTalkSize;

	auto seq = cocos2d::Sequence::create(
		cocos2d::DelayTime::create(1),
		cocos2d::CallFunc::create([]() {
		DDZGameLogic::Instance().lastTalk->strString[0] = '1';
		DDZGameLogic::Instance().lastTalk->strTalkSize = -1;
	}),
		NULL
		);
	m_ActionLayout->stopAllActions();
	m_ActionLayout->runAction(seq);
}
void DDZGameScence::DDZButton_TalkDefine(cocos2d::Ref*, WidgetUserInfo* pUserInfo)
{
	if (!IsInGame())
	{
		return;
	}
	int iUserIdex = utility::parseInt(WidgetFun::getUserInfoValue(pUserInfo, "Idex")) + 1;
	std::string kTaskStr = WidgetFun::getUserInfoValue(pUserInfo, "Txt");
	std::string kTxt = DDZSoundFun::getDefineSound(m_pLocal->GetGender(), utility::toString(iUserIdex));
	sendTalkDefine(m_pLocal->GetChairID(), utility::toString(kTxt, ":", kTaskStr));

	DDZGameLogic::Instance().lastTalk->cbChairID = m_pLocal->GetChairID();
	DDZGameLogic::Instance().lastTalk->cbType = CMD_GR_C_TableTalk::TYPE_DEFINE;
	utility::StringToChar(utility::toString(kTxt, ":", kTaskStr), DDZGameLogic::Instance().lastTalk->strString, 128);
	DDZGameLogic::Instance().lastTalk->strTalkSize = 1;
	WidgetFun::setVisible(this, "GameTalkPlane", false);
	auto seq = cocos2d::Sequence::create(
		cocos2d::DelayTime::create(1),
		cocos2d::CallFunc::create([]() {
		DDZGameLogic::Instance().lastTalk->strString[0] = '1';
		DDZGameLogic::Instance().lastTalk->strTalkSize = -1;
		}),
		NULL
	);
	m_ActionLayout->stopAllActions();
	m_ActionLayout->runAction(seq);
}
void DDZGameScence::DDZButton_Send_TalkStr(cocos2d::Ref*, WidgetUserInfo* pUserInfo)
{
	if (!IsInGame())
	{
		return;
	}
	std::string kTxt = WidgetFun::getEditeText(this, "GameTalkEdit");
	if (kTxt.length() <= 0)
	{
		return;
	}
	sendTalkString(m_pLocal->GetChairID(), kTxt);
	WidgetFun::setVisible(this, "GameTalkPlane", false);
	WidgetFun::setEditeText(this, "GameTalkEdit", "");

	DDZGameLogic::Instance().lastTalk->cbChairID = m_pLocal->GetChairID();
	DDZGameLogic::Instance().lastTalk->cbType = CMD_GR_C_TableTalk::TYPE_WORD;
	utility::StringToChar(kTxt, DDZGameLogic::Instance().lastTalk->strString, 128);
	DDZGameLogic::Instance().lastTalk->strTalkSize = 1;
	auto seq = cocos2d::Sequence::create(
		cocos2d::DelayTime::create(1),
		cocos2d::CallFunc::create([]() {
		DDZGameLogic::Instance().lastTalk->strString[0] = '1';
		DDZGameLogic::Instance().lastTalk->strTalkSize = -1;
	}),
		NULL
		);
	m_ActionLayout->stopAllActions();
	m_ActionLayout->runAction(seq);
}
void DDZGameScence::DDZHNMJButton_BiaoQing(cocos2d::Ref*, WidgetUserInfo* pUserInfo)
{
	if (!IsInGame())
	{
		return;
	}
	std::string kFile = WidgetFun::getUserInfoValue(pUserInfo, "File");
	sendTalkBiaoQing(m_pLocal->GetChairID(), kFile);
	DDZGameLogic::Instance().lastTalk->cbChairID = m_pLocal->GetChairID();
	DDZGameLogic::Instance().lastTalk->cbType = CMD_GR_C_TableTalk::TYPE_BIAOQING;
	utility::StringToChar(kFile, DDZGameLogic::Instance().lastTalk->strString, 128);
	DDZGameLogic::Instance().lastTalk->strTalkSize = 1;
	WidgetFun::setVisible(this, "GameTalkPlane", false);
	auto seq = cocos2d::Sequence::create(
		cocos2d::DelayTime::create(1),
		cocos2d::CallFunc::create([]() {
		DDZGameLogic::Instance().lastTalk->strString[0] = '1';
		DDZGameLogic::Instance().lastTalk->strTalkSize = -1;
	}),
		NULL
		);
	m_ActionLayout->stopAllActions();
	m_ActionLayout->runAction(seq);
}
void DDZGameScence::DDZButton_RobDizhu(cocos2d::Ref*, WidgetUserInfo* pUserInfo)
{
	if (!IsInGame())
	{
		return;
	}
	CMD_C_CallScore kRaiseRatio;
	kRaiseRatio.cbCallScore = utility::parseInt(WidgetFun::getUserInfoValue(pUserInfo, "RaiseRatio"));
	SendSocketData(SUB_C_CALL_SCORE, &kRaiseRatio, sizeof(kRaiseRatio));
}
void DDZGameScence::DDZButton_GetDizhu(cocos2d::Ref*, WidgetUserInfo* pUserInfo)
{
	if (!IsInGame())
	{
		return;
	}
	CMD_C_CallScore kCallScore;
	kCallScore.cbCallScore = utility::parseInt(WidgetFun::getUserInfoValue(pUserInfo, "CallScore"));
	SendSocketData(SUB_C_CALL_SCORE, &kCallScore, sizeof(kCallScore));
}